<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="java.util.ArrayList"
    import="ojt.model.Shain"
    import="ojt.config.Sections"
%>
<%
//リクエストスコープから、フォームの入力情報を取得する。
//入力されていなければ、空文字列をセットする。
ArrayList<Shain> searchResult = (ArrayList<Shain>)request.getAttribute("searchResult");
ArrayList<String> errorMessages = (ArrayList<String>)request.getAttribute("errorMessages");
String shain_no = request.getAttribute("shain_no") != null ? (String)request.getAttribute("shain_no") : "";
String sex = request.getAttribute("sex") != null ? (String)request.getAttribute("sex") : "";
String section_code = request.getAttribute("section_code") != null ? (String)request.getAttribute("section_code") : "";
String enter_date_from = request.getAttribute("enter_date_from") != null ? (String)request.getAttribute("enter_date_from") : "";
String enter_date_to = request.getAttribute("enter_date_to") != null ? (String)request.getAttribute("enter_date_to") : "";
int total = request.getAttribute("searchResult") != null ? searchResult.size(): 0 ;
String selected = new String("selected=\"selected\"");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- css -->
<link href="common/css/style.css" media="all" rel="stylesheet" type="text/css"  />
<link href="common/css/tablesorter.css" media="all" rel="stylesheet" type="text/css"  />
<!-- js -->
<script src="common/js/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="common/js/jquery.tablesorter.js" type="text/javascript"></script>
<script src="common/js/util.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("#shainTable").tablesorter(
			{headers:{
				1: {sorter: false},
				2: {sorter: false},
				4: {sorter: false},
				5: {sorter: false},
				6: {sorter: false}
			},widgets: ['zebra']});
	});
</script>
<title>社員情報検索画面</title>
</head>
<body>
<!-- wrapper start -->
<div id="wrapper">

<h1>EMS[Employee Management System]</h1>
<div id="header_left">
	<a href="MenuServlet">メニューへ戻る</a>
</div>
<div id="header_right">
	<% out.print(session.getAttribute("userName") + "さん\n"); %>
	<a href="LogoutServlet">ログアウト</a>
</div>
<h2>社員情報検索　一覧（検索）</h2>

<!-- errorMessage start -->
<div id="errorMessage">
<%
if(errorMessages != null && errorMessages.isEmpty() == false){
	for(String error : errorMessages) out.println(error + "<br />");
}%>
</div>
<!-- errorMessage end -->

<!-- searchArea start -->
<h4>検索条件</h4>
<div id="searchArea">
<form method="post" action="SearchServlet">
<dl>
	<dt>社員番号</dt>
	<dd><input type="text" name="shain_no" value="<%=shain_no %>" size="5" maxlength="5" /></dd>
	<dt>性別</dt>
	<dd>
		<select name="sex">
		<option value=""></option>
		<option value="0" <% if(sex.equals("0")){out.print(selected);}%>>男性</option>
		<option value="1" <% if(sex.equals("1")){out.print(selected);}%>>女性</option>
	</select>
	</dd>
	<dt>部門</dt>
	<dd>
		<select name="section_code" >
			<option value=""></option>
<%
//Enum型　Sectionsから文字列を取得
for(Sections s : Sections.values()){
	out.print("\t\t\t<option value=\"" + s +"\"");
	if(section_code.equals(s.toString())){
		out.print("selected");
	}
	out.print(">"+ s + "</option>\n");
}
%>
		</select>
	</dd>
	<br />
	<br />
	<dt>入社年月日(From)</dt>
	<dd><input type="text" name="enter_date_from" value="<%= enter_date_from %>" size="5" maxlength="8" />～</dd>
	<dt>入社年月日(To)</dt>
	<dd><input type="text" name="enter_date_to" value="<%= enter_date_to %>" size="5" maxlength="8" /></dd>
	<input type="submit" value="検索" />
</dl>
</form>
</div>
<!-- searchArea end -->

<h4>検索結果</h4>
<!-- searchResultArea start -->
<div id="searchResultArea">

<p>検索結果：全<%= total %>件</p>
<table id="shainTable" class="tablesorter" border="1">
<thead>
<tr>
	<th width="12%">社員番号</th>
	<th>名前</th>
	<th>性別</th>
	<th width="15%">入社年月日</th>
	<th>部門</th>
	<th>メールアドレス</th>
	<th colspan="3">操作</th>
</tr>
</thead>
<tbody>
<%if (searchResult !=null && searchResult.isEmpty() == false){
	for(Shain shain : searchResult){
		out.print("<tr>");
		out.print("<td>" + shain.shain_no +"</td>");
		out.print("<td>" + shain.first_name + shain.last_name + "</td>");
		//0→男性、1→女性に変換
		out.print(shain.sex.equals("0") ? "<td>男性</td>" : "<td>女性</td>");
		//yyyyMMdd形式からyyyy年MM月dd日形式に変換して表示
		out.print("<td>" + shain.enter_date.replaceAll("([0-9]{4})([0-9]{2})([0-9]{2})", "$1年$2月$3日") +"</td>");
		out.print("<td>" + shain.section_code +"</td>");
		out.print("<td>" + shain.e_mail +"</td>");
		out.print("<td><a href=\"SearchDetailServlet?shain_no=" + shain.shain_no + "\">詳細</a>");
		out.print("<td><a href=\"UpdateServlet?shain_no=" + shain.shain_no + "\">更新</a>");
		out.print("<td><a href=\"javascript:void(0)\" onclick=\"deleteConfirm(\'"+shain.shain_no + "\')\">削除</a>");
		out.print("</tr>\n");
	}
} %>
</tbody>
</table>
</div>
<!-- searchResultArea end -->

</div>
<!-- wrapper end-->
</body>
</html>